/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.talbd.proventas;

import edu.upn.talbd.proventas.dal.EmpleadoDAO;
import edu.upn.talbd.proventas.entidades.Empleado;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrador
 */
public class BuscarEmpleado extends javax.swing.JDialog {

    /**
     * Creates new form BuscarEmpleado
     */
    
    private Empleado seleccion;
    
    public BuscarEmpleado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        ArrayList<Empleado> coleccion = new ArrayList<Empleado>();
        String nombreBuscar = txtNombreBuscarEmpleado.getText();

        //ProveedorDAO
        EmpleadoDAO objEmpleadoDAO = new EmpleadoDAO();
        Empleado objEmpleado = new Empleado();
        objEmpleado.setNombre(nombreBuscar);
        coleccion = objEmpleadoDAO.buscar(objEmpleado);
        DefaultTableModel temp = (DefaultTableModel) tblEmpleados.getModel();
        temp.setRowCount(0);
        for(Empleado e : coleccion){
            //System.out.println(“Nombre del Empleado: ” + tmpObjeto.getNombre());
            Object nuevo[] ={e.getId(), e.getNombre(), e.getApellidoPaterno(), e.getApellidoMaterno()};
            temp.addRow(nuevo);
        }
        setSeleccion(new Empleado(0, "", "","","", "", "", BigDecimal.ZERO, ""));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        txtNombreBuscarEmpleado = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEmpleados = new javax.swing.JTable();
        btnBuscarCliente = new javax.swing.JButton();
        btnSeleccionarProveedor = new javax.swing.JButton();
        lblrestriccionSeleccion = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Empleado");

        jLabel5.setText("Nombre:");

        tblEmpleados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nombre", "Apellido Paterno", "Apellido Materno"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblEmpleados);

        btnBuscarCliente.setText("Buscar");
        btnBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarClienteActionPerformed(evt);
            }
        });

        btnSeleccionarProveedor.setText("Seleccionar");
        btnSeleccionarProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarProveedorActionPerformed(evt);
            }
        });

        lblrestriccionSeleccion.setForeground(new java.awt.Color(255, 0, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(txtNombreBuscarEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnBuscarCliente))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(190, 190, 190)
                        .addComponent(btnSeleccionarProveedor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblrestriccionSeleccion, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtNombreBuscarEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscarCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblrestriccionSeleccion, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(btnSeleccionarProveedor))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarClienteActionPerformed
        // TODO add your handling code here:
        ArrayList<Empleado> coleccion = new ArrayList<Empleado>();
        String nombreBuscar = txtNombreBuscarEmpleado.getText();

        //ProveedorDAO
        EmpleadoDAO objEmpleadoDAO = new EmpleadoDAO();
        Empleado objEmpleado = new Empleado();
        objEmpleado.setNombre(nombreBuscar);
        coleccion = objEmpleadoDAO.buscar(objEmpleado);
        DefaultTableModel temp = (DefaultTableModel) tblEmpleados.getModel();
        temp.setRowCount(0);
        for(Empleado e : coleccion){
            //System.out.println(“Nombre del Empleado: ” + tmpObjeto.getNombre());
            Object nuevo[] ={e.getId(), e.getNombre(), e.getApellidoPaterno(), e.getApellidoMaterno()};
            temp.addRow(nuevo);
        }
    }//GEN-LAST:event_btnBuscarClienteActionPerformed
    
    private void btnSeleccionarProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarProveedorActionPerformed
        // TODO add your handling code here:
        TableModel tableModel = tblEmpleados.getModel();
        if(tblEmpleados.getSelectedRow() == -1){
            lblrestriccionSeleccion.setText("*Seleccione un empleado");
        }
        else{
            int fila = tblEmpleados.getSelectedRow();
            setSeleccion(new Empleado(Integer.parseInt(tableModel.getValueAt(fila, 0).toString()), tableModel.getValueAt(fila, 1).toString(), tableModel.getValueAt(fila, 2).toString(),"","", "", "", BigDecimal.ZERO, ""));
            this.dispose();
        }
    }//GEN-LAST:event_btnSeleccionarProveedorActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarCliente;
    private javax.swing.JButton btnSeleccionarProveedor;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblrestriccionSeleccion;
    private javax.swing.JTable tblEmpleados;
    private javax.swing.JTextField txtNombreBuscarEmpleado;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the seleccion
     */
    public Empleado getSeleccion() {
        return seleccion;
    }

    /**
     * @param seleccion the seleccion to set
     */
    public void setSeleccion(Empleado seleccion) {
        this.seleccion = seleccion;
    }
}
